<?php
class PrivilegeModel {
	private $_aAuth = array ('yes' => '需要', 'no' => '不需要' );
	private $_sSortName = 'art_post_time';
	private $_sSortType = 'desc';
	
	public function getNoAuth() {
		$aData = Base::$oBase->Cache_get ( 'privilege_noauth' );
		if ($aData == '__NOCACHE__') {
			$sSql = 'SELECT pri_value val FROM privileges WHERE pri_auth = ?';
			Base::$oBase->Db_query ( $sSql, 'no' );
			$aData = array ();
			while ( ($aRow = Base::$oBase->Db_getRow ()) !== false ) {
				$aValue = unserialize ( $aRow ['val'] );
				foreach ( $aValue as $sModel => $aMethod ) {
					if (is_array ( $aData [$sModel] )) {
						$aData [$sModel] = array_merge ( $aData [$sModel], $aMethod );
						$aData [$sModel] = array_unique ( $aData [$sModel] );
					} else {
						$aData [$sModel] = $aMethod;
					}
				}
			}
			Base::$oBase->Cache_set ( 'privilege_noauth', $aData );
		}
		return $aData;
	}
	
	public function add($aInfo) {
		if (is_array ( $this->get ( $aInfo ['name'], true ) )) {
			return false;
		}
		$sSql = 'INSERT INTO privileges
				SET
					pri_name = ?,
					pri_desc = ?,
					pri_auth = ?,
					pri_value = ?';
		Base::$oBase->Db_exec ( $sSql, array ($aInfo ['name'], $aInfo ['desc'], $aInfo ['auth'], serialize ( $aInfo ['method'] ) ) );
		Base::$oBase->Cache_del ( 'privileges' );
		Base::$oBase->Cache_del ( 'privilege_noauth' );
		return Base::$oBase->Db_getInsertId ();
	}
	
	public function edit($aInfo) {
		$aInfo2 = $this->get ( $aInfo ['name'], true );
		if ($aInfo2 && $aInfo ['id'] != $aInfo2 ['id']) {
			return false;
		}
		$sSql = 'UPDATE privileges
				SET
					pri_name = ?,
					pri_desc = ?,
					pri_auth = ?,
					pri_value = ?
				WHERE
					pri_id = ?';
		Base::$oBase->Db_exec ( $sSql, array ($aInfo ['name'], $aInfo ['desc'], $aInfo ['auth'], serialize ( $aInfo ['method'] ), $aInfo ['id'] ) );
		Base::$oBase->Cache_del ( 'privileges' );
		Base::$oBase->Cache_del ( 'privilege_noauth' );
	}
	
	public function del($iPrivilegeId) {
		$sSql = 'DELETE FROM privileges WHERE pri_id = ?';
		Base::$oBase->Db_exec ( $sSql, $iPrivilegeId );
		Base::$oBase->Cache_del ( 'privileges' );
	}
	
	public function get($iPrivilegeId, $bName = false) {
		if ($bName) {
			$sSql = 'SELECT pri_id FROM privileges WHERE pri_name = ?';
			$iPrivilegeId = Base::$oBase->Db_getOne ( $sSql, $iPrivilegeId );
		}
		$aAll = $this->_getAll ();
		return $aAll [$iPrivilegeId];
	}
	
	private function _getAll() {
		$aData = Base::$oBase->Cache_get ( 'privileges' );
		if ($aData == '__NOCACHE__') {
			$aData = array ();
			$sSql = 'SELECT * FROM privileges';
			Base::$oBase->Db_query ( $sSql );
			while ( ($aRow = Base::$oBase->Db_getRow ()) !== false ) {
				$aData [$aRow ['pri_id']] = array ('id' => $aRow ['pri_id'] );
				$aData [$aRow ['pri_id']] ['name'] = $aRow ['pri_name'];
				$aData [$aRow ['pri_id']] ['desc'] = $aRow ['pri_desc'];
				$aData [$aRow ['pri_id']] ['auth'] = $aRow ['pri_auth'];
				$aData [$aRow ['pri_id']] ['method'] = unserialize ( $aRow ['pri_value'] );
			}
			Base::$oBase->Cache_set ( 'privileges', $aData, null, 0 );
		}
		return $aData;
	}
	
	public function getList($iPage = 1, $iLimit = 20) {
		$sSql = 'SELECT pri_id id, pri_name name, pri_desc `desc`, pri_auth auth FROM privileges';
		$aList = Base::$oBase->Db_getList ( $sSql, null, $iPage, $iLimit );
		foreach ( $aList ['data'] as $iKey => $aRow ) {
			$aList ['data'] [$iKey] ['auth_value'] = $this->_aAuth [$aRow ['auth']];
		}
		return $aList;
	}
	
	public function getListNeedAuth() {
		if (in_array ( $_SESSION ['user'] ['id'], preg_split ( '/[,\s]+/s', Base::getConf ( 'base.admin' ) ) )) {
			$sSql = 'SELECT pri_id id, pri_name name, pri_desc `desc`, pri_auth auth FROM privileges WHERE pri_auth = ? ORDER BY pri_name';
			$aList = Base::$oBase->Db_getList ( $sSql, 'yes' );
		} else {
			$sSql = 'SELECT pri_id id, pri_name name, pri_desc `desc`, pri_auth auth 
					FROM privileges, authorize 
					WHERE pri_auth = ? AND pri_id = aut_pri_id  AND aut_use_id = ?
					ORDER BY pri_name';
			$aList = Base::$oBase->Db_getList ( $sSql, array ('yes', $_SESSION ['user'] ['id'] ) );
		}
		foreach ( $aList ['data'] as $iKey => $aRow ) {
			$aList ['data'] [$iKey] ['auth_value'] = $this->_aAuth [$aRow ['auth']];
		}
		return $aList ['data'];
	}
	
	public function getInfo($iPId) {
		$aData = self::_getAllInfo ();
		return $aData [$iPId];
	}
	
	private function _getAllInfo() {
		$aData = Base::$oBase->Cache_get ( 'privilege' );
		if ($aData == '__NOCACHE__') {
			$aData = array ();
			$sSql = 'SELECT pri_id id, pri_name name, pri_value `value`
					FROM privilege
					ORDER BY name';
			Base::$oBase->Db_query ( $sSql );
			while ( ($aRow = Base::$oBase->Db_getRow ()) !== false ) {
				$aData [$aRow ['id']] = $aRow;
				$aData [$aRow ['id']] ['privilege'] = unserialize ( $aRow ['value'] );
			}
			Base::$oBase->Cache_set ( 'privilege', $aData, null, 0 );
		}
		return $aData;
	}
	
	public function clean($aMethod) {
		$aPrivilege = array ();
		if (is_array ( $aMethod )) {
			foreach ( $aMethod as $sMethod ) {
				list ( $sModel, $sMethod ) = explode ( '.', $sMethod );
				$aPrivilege [$sModel] [] = $sMethod;
			}
			foreach ( $aPrivilege as $sKey => $aRow ) {
				if (in_array ( '__FULL__', $aRow )) {
					unset ( $aPrivilege [$sKey] );
					$aPrivilege [$sKey] [] = '__FULL__';
				} else {
					$aRow = array_unique ( $aRow );
					sort ( $aRow );
					$aPrivilege [$sKey] = $aRow;
				}
			}
		}
		ksort ( $aPrivilege );
		return $aPrivilege;
	}
	
	public function setSort($sName, $sType) {
		if ($sName) {
			$this->_sSortName = 'pri_' . $sName;
			if ($sType == 'desc') {
				$this->_sSortType = 'desc';
			} else {
				$this->_sSortType = 'asc';
			}
		}
	}
}
?>